//
//  RegisterViewController.m
//  AI
//
//  Created by zjhc on 2018/1/8.
//  Copyright © 2018年 zjhc. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()


@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    
    CGRect  frame = CGRectMake(-15, 0, 14, 14);
    // 设置右侧搜索图标
    UIButton *rightView  = [[ UIButton alloc]initWithFrame:frame];
    [rightView setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];

    UIView *uv = [[UIView alloc]initWithFrame:frame];
    [uv addSubview:rightView];
    self.area.rightViewMode = UITextFieldViewModeAlways;
    self.area.rightView = uv;
    rightView.userInteractionEnabled = YES;

    [rightView addTarget:self action:@selector(searchArea:) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置密码显隐
    CGRect  pwdFrame = CGRectMake(-15, 0, 16, 12);
    UIButton *pwdRightView  = [[ UIButton alloc]initWithFrame:pwdFrame];
    [pwdRightView setBackgroundImage:[UIImage imageNamed:@"eye"] forState:UIControlStateNormal];
    
    UIView *pwduv = [[UIView alloc]initWithFrame:pwdFrame];
    [pwduv addSubview:pwdRightView];
    self.passWord.rightViewMode = UITextFieldViewModeAlways;
    self.passWord.rightView = pwduv;
    pwdRightView.userInteractionEnabled = YES;//有问题

    [pwdRightView addTarget:self action:@selector(isVisible:) forControlEvents:UIControlEventTouchUpInside];
    
    self.registerNow.layer.cornerRadius = 8.0;
    self.getSmsCode.layer.cornerRadius = 8.0;
    
    self.area.horizontalPadding = 15;
    self.name.horizontalPadding = 15;
    self.phone.horizontalPadding = 15;
    self.smsAuthCode.horizontalPadding = 15;
    self.passWord.horizontalPadding = 15;

 }

-(void)searchArea:(UIButton*)sender {
    
    
}

#pragma mark - 设置密码显隐
- (void)isVisible:(UIButton*)sender {
    self.passWord.secureTextEntry = !self.passWord.secureTextEntry;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - 获取短信验证码
- (IBAction)getSmsCodeAction:(id)sender {
    
    if (![self mathPhone:self.phone.text]) {
        return;
    }
    
    [sender startCountDownWithSecond:10];
    
    [WTHttpRequest requestJsonPostWithUrlStr:[UrlManage sharedManager].smsSend params:@{@"telphone":self.phone.text} onComplete:^(NSError *error, NSDictionary *resultJson) {
        
        if (!error) {
            
        } else {
            [HXAlertViewEx showInTitle:@"提示" Message:error.domain ViewController:self];
        }
    }];
    
    [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
        [countDownButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        NSString *title = [NSString stringWithFormat:@"%zd",second];
        return title;
    }];
    
    [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
        countDownButton.enabled = YES;
        [countDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        return @"重新发送";
    }];
}

#pragma mark -注册按钮事件
- (IBAction)registerAction:(id)sender {
    
//    userName：用户名不允许为空
//    telphone：电话号码不允许为空，数字
//    password：用户密码、不允许为空
//    provinceId：省id，不允许为空，数字
//    cityId：市id 不允许为空，数字
//    areaId：区县id 不允许为空，数字
//    storesId：营业厅id 不允许为空，数字
//    smsCode：短信验证码 不允许为空
//    POST数据：请求体参数，不允许为空，其中header须添加：Content-type="application/json"，示例如下：
//    {
//        "telphone":"1333333333",
//        "userName":"aassdd",
//        "provinceId":1,
//        "cityId":2,
//        "areaId":3,
//        "storesId":1,
//        "smsCode":1234,
//        "password":"123124"
//    }
    
//    tlet UITextField *area;
//    @property (weak, nonatomic) IBOutlet UITextField *name;
//    @property (weak, nonatomic) IBOutlet UITextField *phone;
//    @property (weak, nonatomic) IBOutlet UITextField *smsAuthCode;
//    @property (weak, nonatomic) IBOutlet UITextField *passWord;
//    @property (weak, nonatomic) IBOutlet UIButton *registerNow;
//    @property (weak, nonatomic) IBOutlet JKCountDownButton *getSmsCode;
    
    [self registerService];
    
    
}

#pragma mark -注册接口服务
- (void) registerService {
    
    NSDictionary *params = @{@"telphone":self.phone.text,@"userName":self.name.text,@"provinceId":@"31",@"cityId":@"383",@"areaId":@"3232",@"smsCode":self.smsAuthCode.text,@"password":self.passWord.text,@"storesId":@"1"};

    [WTHttpRequest requestJsonPostWithUrlStr:[UrlManage sharedManager].registerApp params:params onComplete:^(NSError *error, NSDictionary *resultJson) {
        
        if (!error) {
            
        } else {
            [HXAlertViewEx showInTitle:@"提示" Message:error.domain ViewController:self];
        }
    }];
}

-(BOOL)mathPhone:(NSString *)phone{
    
    if (!phone || [phone isEqualToString:@""]) {
        [HXAlertViewEx showInTitle:@"提示" Message:@"请输入手机号" ViewController:self];
        return NO;
    }
    
    if (![HXNSStringUtil isMobilePhoneNumber:phone]) {
        
        [HXAlertViewEx showInTitle:@"提示" Message:@"手机号格式不正确,请重新输入" ViewController:self];
        return NO;
        
    }
    return YES;
}

@end
